package main

import (
    "database/sql"
    "fmt"
    "go.uber.org/zap"
    "laxkits/lib/laxmysql"
    "laxkits/lib/laxutils"
)

func main() {
    if err := laxutils.RoInit(); err != nil {
        return
    }
    defer laxutils.AnyKeyContinue()
    defer laxutils.RoUninit()
    if len(laxutils.ROGlobalConfigNode.RoleIdList) < 2 {
        zap.S().Error("RoleIdList需要两个参数,第1个的源玩家,第2个为目的玩家")
        return
    }
    conn := laxmysql.GetLaxMySQLMgr().GetById("1")
    if conn == nil {
        zap.S().Error("找不到数据库链接")
        return
    }
    ignoreFields := []string{
        "id",
        "name",
        "account",
        "nickid",
        "ServerId",
        "RoleBrief",
    }
    var roleFields []string
    if err := conn.LaxQuery("select COLUMN_NAME from information_schema.COLUMNS where TABLE_SCHEMA = 'player0' and TABLE_NAME = 'role';",
        func(row *sql.Rows) error {
            var name string
            if err := row.Scan(&name); err != nil {
                zap.S().Warnf("scan 出错[%s]", err)
                return err
            }
            for _, v := range ignoreFields {
                if v == name {
                    return nil
                }
            }
            roleFields = append(roleFields, name)
            return nil
        }); err != nil {
        zap.S().Errorf("解析role表出错:[%s]", err)
        return
    }
    updSql := fmt.Sprintf("update player%d.role t1, player%d.role t2 set ",
        laxutils.ROGlobalConfigNode.RoleIdList[1]%5,
        laxutils.ROGlobalConfigNode.RoleIdList[0]%5)
    for idx, field := range roleFields {
        if idx > 0 {
            updSql += ","
        }
        updSql += fmt.Sprintf("t1.%s = t2.%s", field, field)
    }
    updSql += fmt.Sprintf(" where t1.id = %d and t2.id = %d",
        laxutils.ROGlobalConfigNode.RoleIdList[1],
        laxutils.ROGlobalConfigNode.RoleIdList[0])
    if affect, err := conn.LaxExecute(updSql); err != nil {
        zap.S().Errorf("更新出错:[%s]", err)
    } else {
        zap.S().Infof("更新成功,影响行数:[%d]", affect)
    }
}
